home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / printing / findprinter / currentprinter.c next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  6.3 KB  |  220 lines

  1. /*
  2.     File:        CurrentPrinter.c
  3.  
  4.     Contains:    Check the default printer under GX or old printing architecture.
  5.                 Print out the name, entityType, and zone.
  6.     
  7.                 This sample is provided as-is. It's guaranteed to work on my
  8.                 mac this instant, and nothing more. It shows how to determine
  9.                 the currently selected printer now. Since this isn't an officially
  10.                 supported endeavor, things may change in the future, and this
  11.                 snippet will be for naught.
  12.  
  13.     Written by: Dave Polaschek and David Hayward    
  14.  
  15.     Copyright:    Copyright © 1995-1999 by Apple Computer, Inc., All Rights Reserved.
  16.  
  17.                 You may incorporate this Apple sample source code into your program(s) without
  18.                 restriction. This Apple sample source code has been provided "AS IS" and the
  19.                 responsibility for its operation is yours. You are not permitted to redistribute
  20.                 this Apple sample source code as "Apple sample source code" after having made
  21.                 changes. If you're going to re-distribute the source, we require that you make
  22.                 it clear in the source that the code was descended from Apple sample source
  23.                 code, but that you've made changes.
  24.  
  25.     Change History (most recent first):
  26.                 7/23/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1 fixed bug dealing witht
  27.                                             copying more than 256 bytes into a pascal string.
  28.                 
  29.  
  30. */
  31.  
  32.  
  33. #include <stdio.h>
  34. #include <string.h>
  35. #include <Types.h>
  36. #include <TextUtils.h>
  37. #include <Files.h>
  38. #include <Folders.h>
  39. #include <Resources.h>
  40. #include <Aliases.h>
  41. #include <Gestalt.h>
  42. #include <Memory.h>
  43.  
  44.  
  45. #define gestaltGXVersion            'qdgx'            // in PrintingManager.h (old) or GXPrinting.h (new)
  46. #define gestaltGXPrintingMgrVersion    'pmgr'            // in PrintingManager.h (old) or GXPrinting.h (new)
  47. #define gestaltAliasMgrAttr            'alis'            // in Aliases.h
  48.  
  49. #define printingResourceID            0xE000            // 0xE000 = -8192
  50.  
  51.  
  52. OSErr        GetCurrentPrinter(Str255 printer);
  53. OSErr        test(void);
  54. Boolean        gxInstalled(void);
  55. Boolean        validPrinter(void);
  56.  
  57.  
  58. /*------------------------------------------------------------------------------*\
  59.         This function returns true if QuickDraw GX printing is available
  60. \*------------------------------------------------------------------------------*/
  61. Boolean gxInstalled(void)
  62. {
  63.     /*
  64.     long version;
  65.  
  66.     if (Gestalt(gestaltGXVersion, &version) == noErr)
  67.         if (Gestalt(gestaltGXPrintingMgrVersion, &version) == noErr)
  68.             return(true);
  69.     return(false);
  70.     */
  71.     return false;// GX printing no longer supported
  72. }
  73.  
  74.  
  75. /*------------------------------------------------------------------------------*\
  76.         This function returns the network entity of the currently 
  77.         selected printer, and as an added bonus, you get an error code, too!
  78. \*------------------------------------------------------------------------------*/
  79. OSErr GetCurrentPrinter(Str255 printer)
  80. {
  81.     StringHandle    theDriver;
  82.     Handle            thePrinter;
  83.     OSErr            theErr;
  84.     short            resFileRefnum;
  85.  
  86.     theDriver = GetString(printingResourceID);
  87.     if (!theDriver) goto BADDriver;
  88.  
  89.     if (gxInstalled()) {
  90.         short    vRefNum;
  91.         long    dirID;
  92.         FSSpec    theFile;
  93.         long    resSize;
  94.  
  95.         theErr=FindFolder(kOnSystemDisk,kDesktopFolderType,kDontCreateFolder,&vRefNum,&dirID);
  96.         if (theErr != noErr) goto BADDriver;
  97.  
  98.         theErr=FSMakeFSSpec(vRefNum,dirID,*theDriver,&theFile);
  99.         if (theErr != noErr) goto BADDriver;
  100.  
  101.         resFileRefnum = FSpOpenResFile(&theFile,fsRdPerm);
  102.         if (resFileRefnum == -1) goto BADResFile;
  103.  
  104.         thePrinter = Get1Resource('comm', 0);
  105.         if (!thePrinter) goto BADPrinter;
  106.  
  107.         if (*((long *)*thePrinter) != 'PPTL') goto BADPrinter;
  108.  
  109.         resSize = GetHandleSize(thePrinter)-6;
  110.         BlockMoveData((*thePrinter)+6,printer,resSize);
  111.     } else {
  112.         Boolean     aliasCallsPresent;
  113.         Boolean     aliasResourcePresent;
  114.         long        resSize,response;
  115.         AliasHandle    theAlias;
  116.         
  117.         aliasCallsPresent = ((Gestalt(gestaltAliasMgrAttr,&response) == noErr) &&
  118.             (response & 1));
  119.  
  120.         theAlias = (AliasHandle) GetResource('alis',printingResourceID);
  121.         aliasResourcePresent = !!(theAlias);
  122.         
  123.         if (aliasCallsPresent && aliasResourcePresent) {
  124.             FSSpec    fileSpec;
  125.             Boolean    wasChanged;
  126.  
  127.             theErr = ResolveAlias(NULL, theAlias,&fileSpec,&wasChanged);
  128.             if (theErr != noErr) goto BADDriver;
  129.  
  130.             resFileRefnum = FSpOpenResFile(&fileSpec,fsRdPerm);
  131.             if (resFileRefnum == -1) goto BADResFile;
  132.         } else {
  133.             resFileRefnum = OpenResFile(*theDriver);
  134.             if (resFileRefnum == -1) goto BADResFile;
  135.         }
  136.         thePrinter = Get1Resource('PAPA', printingResourceID);
  137.         if (!thePrinter) goto BADPrinter;
  138.         resSize = GetHandleSize(thePrinter);
  139.         HLock(thePrinter);
  140.         BlockMoveData(*thePrinter,printer,resSize < 256 ? resSize : 256);
  141.         HUnlock(thePrinter);
  142.     }
  143.     ReleaseResource(thePrinter);
  144.     CloseResFile(resFileRefnum);
  145.  
  146.     return(noErr);
  147.  
  148. // Error returns
  149. BADPrinter:
  150.     CloseResFile(resFileRefnum);
  151. BADResFile:
  152.     theErr = ResError();
  153. BADDriver:
  154.     return(theErr);
  155. }
  156.  
  157.  
  158. /*------------------------------------------------------------------------------*\
  159.         This function is called by the test harness. Doesn't do much except
  160.         prove that things work without crashing.
  161. \*------------------------------------------------------------------------------*/
  162. OSErr test(void)
  163. {
  164.     OSErr    testValue;
  165.     Str255    printerEntity;
  166.  
  167.     testValue = GetCurrentPrinter(printerEntity);
  168.     if (testValue)
  169.         return testValue;
  170.     else {
  171.         Str31            printer,type,zone;
  172.         unsigned char    *currentString;
  173.  
  174.         currentString = printerEntity;
  175.         BlockMove(currentString,printer,StrLength(currentString)+1);
  176.         p2cstr(printer);
  177.         
  178.         currentString += StrLength(currentString) + 1;
  179.         BlockMove(currentString,type,StrLength(currentString) + 1);
  180.         p2cstr(type);
  181.         
  182.         currentString += StrLength(currentString) + 1;
  183.         BlockMove(currentString,zone,StrLength(currentString) + 1);
  184.         p2cstr(zone);
  185.  
  186.         printf("Current Printer name = %s\n"
  187.                 "EntityType = %s\n"
  188.                 "Zone = %s\n",printer,type,zone);
  189.         return noErr;
  190.     }
  191. }
  192.  
  193.  
  194. /*------------------------------------------------------------------------------*\
  195.         This function tells if there's a valid printer selected (i.e. not
  196.         aimed at a driver that's since been deleted or no printer selected
  197.         since last System sw install) in the chooser
  198. \*------------------------------------------------------------------------------*/
  199. Boolean validPrinter(void)
  200. {
  201.     Str255    printerEntity;
  202.     
  203.     if (GetCurrentPrinter(printerEntity) != noErr)
  204.         return false;
  205.     else if (StrLength(printerEntity) == 0)
  206.         return false;
  207.     else
  208.         return true;
  209. }
  210.  
  211. void main(void)
  212. {
  213.     OSErr    testResult;
  214.     
  215.     testResult = test();
  216.  
  217.     printf("Return value from test() = %d\n",testResult);
  218. }
  219.  
  220.